package com.henu.engineer.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author noexcept
 * @date 2021/7/23 0:24
 */
public class RequestInterceptor implements HandlerInterceptor {
    /**
     * 对除登录以外的所有请求进行验证
     * 判断是否登录,只有登陆了才能执行一些请求。
     * @see WebConfig#addInterceptors(InterceptorRegistry)
     * @see com.henu.engineer.controller.LoginController#Login(HttpSession, HttpServletRequest, String, String)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        HttpSession session = request.getSession();
        Boolean isLogged = (Boolean) session.getAttribute("isLogged");
        if (isLogged != null) {
            return isLogged;
        } else return false;
    }
}
